/*
 * QuickSort.java
 *
 * Created on February 10, 2009, 12:13 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kernel;

import java.util.Vector;

/**
 *
 * @author alvaro
 */
public class QuickSort {
    
    private QuickSortElement[]array;
    
    /**
     * Creates a new instance of QuickSort
     */
    public QuickSort(QuickSortElement[] array) {
        this.array = array;
    }
    
    public void order(){
        this.order(array, 0, array.length-1);
    }
    
    public QuickSortElement[] getArray(){
        return this.array;
    }
    
    private void order(QuickSortElement[] vector, int primero, int ultimo){
        
    	int i=primero, j=ultimo;
        
    	QuickSortElement pivote = vector[(primero+ultimo)/2];
    	QuickSortElement auxiliar;
 
    	do{
    		while(vector[i].compareTo(pivote) < 0) i++;    		
    		while(vector[j].compareTo(pivote) > 0) j--;
 
    		if (i<=j){
    			auxiliar=vector[j];
    			vector[j]=vector[i];
    			vector[i]=auxiliar;
    			i++;
    			j--;
    		}
 
    	} while (i<=j);
 
    	if(primero<j) order(vector, primero, j);
    	if(ultimo>i) order(vector, i, ultimo);
    }

    public void imprimirLista(){
        for(int i = 0;i<array.length;i++){
            System.out.println(array[i].toString());
        }
    }
    
    public static void main(String arg[]){
        Documento doc1 = new Documento(4, "palabra 4");
        Documento doc2 = new Documento(2, "palabra 2");
        Documento doc3 = new Documento(5, "palabra 5");
        Documento doc4 = new Documento(7, "palabra 7");
        Documento doc5 = new Documento(1, "palabra 1");
        
        Vector<Documento> vec = new Vector<Documento>();
        vec.add(doc1);
        vec.add(doc2);
        vec.add(doc3);
        vec.add(doc4);
        vec.add(doc5);
        
        QuickSort q = new QuickSort((QuickSortElement[]) vec.toArray(new Documento[]{}));
        q.imprimirLista();
        System.out.println("asdf");
        q.order();
        q.imprimirLista();
    }

}
